package me.jiatao.wan.di

import android.app.Application
import android.content.Context
import coil.ImageLoader
import coil.request.CachePolicy
import coil.util.CoilUtils
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import javax.inject.Singleton

/**
 * @Author: taojia
 * @CreateDate: 2021/8/27 10:36 上午
 * @Version: 1.0
 * @Description: 用于提供全局单例对象
 */

@Module
@InstallIn(SingletonComponent::class)
class AppModule {

    @Provides
    @Singleton
    fun provideImageLoader(
        @ApplicationContext context: Context,
        //loggingInterceptor: HttpLoggingInterceptor
    ): ImageLoader {
        return ImageLoader.Builder(context)
            .crossfade(true)
            .memoryCachePolicy(CachePolicy.ENABLED)//设置内存的缓存策略
            .diskCachePolicy(CachePolicy.ENABLED)//设置磁盘的缓存策略
            .networkCachePolicy(CachePolicy.ENABLED)//设置网络的缓存策略
            .availableMemoryPercentage(0.25)
            .okHttpClient {
                /**
                 * 自定义请求数据缓存
                 */
                OkHttpClient.Builder()
                    //.addInterceptor(loggingInterceptor)
                    .cache(CoilUtils.createDefaultCache(context))
                    .build()
            }
            .build()
    }
}